package com.petrooil.core.dao;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Repository("orbeonDao")
public class OrbeonDaoImpl implements OrbeonDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> getForms(Serializable department) {
        String sql = "SELECT app, form, last_modified_by FROM orbeon.orbeon_form_definition WHERE app=? AND deleted <> ? GROUP BY form";
        SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
        query.setParameter(0, department);
        query.setParameter(1, "Y");
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        return query.list();
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> getAllForms() {
        String sql = "SELECT app, form FROM orbeon.orbeon_form_definition WHERE deleted <> ?";//GROUP BY form, app
        SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
        query.setParameter(0, "Y");
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        return query.list();
    }
}
